Git Cheat Sheet
1. Setting Up Git
Initialize a repository
git init
Connect to a GitHub repository
git remote add origin https://github.com/your-username/repo-name.git
Check your configuration
git config --list
Set your username and email
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
2. Basic Commands
Check repository status
git status
Add files to staging
git add .
Commit changes
git commit -m "Your commit message"
Push changes to GitHub
git push origin main
3. Working with Versions
Tag a commit (annotated)
git tag -a v1.0 -m "Version 1.0 release"
Push tags to GitHub
git push origin --tags
List all tags
git tag
Checkout a specific version
git checkout v1.0
4. Undo Changes
Unstage a file
git restore --staged filename
Unstage all files
git restore --staged .
Revert changes to last committed state
git checkout -- filename
Undo the most recent commit (keep changes)
git reset --soft HEAD~1
Undo the most recent commit (discard changes)
git reset --hard HEAD~1
5. Syncing with GitHub
Clone a repository
git clone https://github.com/your-username/repo-name.git
Pull latest changes
git pull origin main
Pull with rebase
git pull --rebase origin main
6. Viewing History
View commit history
git log
View summarized history
git log --oneline
7. Branching (Advanced Use)
Create a new branch
git branch branch-name
Switch to a branch
git checkout branch-name
Merge a branch into main
git checkout main
git merge branch-name
8. Updating Files
Step 1: Check status
git status
Step 2: Stage changes
git add .
Step 3: Commit changes
git commit -m "Update files"
Step 4: Push changes
git push
9. Updating Remote URL
Update the remote URL
git remote set-url origin https://github.com/your-username/new-repo-name.git
Tips
- Use Semantic Versioning:
vMAJOR.MINOR.PATCH
(e.g., v1.0.0
).
- Commit Often: Break work into small, meaningful commits.
- Write Descriptive Commit Messages: Helps track changes.